package com.loveyukari.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.loveyukari.model.VoteGroup;
import com.loveyukari.model.VoteItem;
import com.loveyukari.service.VoteService;

/**
*
*yangshaohua
*2017年5月20日下午5:12:38
**/
@RequestMapping("/admin/vote")
@Controller
public class VoteColtroller {
	
	@Autowired
	private VoteService voteService;

	
	@RequestMapping("/index")
	public String index(Model model){
		VoteGroup group = voteService.getVoteGroup(null);
		if (group == null){
			List<VoteGroup> list = voteService.getVoteGroupList();
			model.addAttribute("list", list);
			return "vote/index";
		} else {
			return "redirect:/admin/vote/getVoteDetail";
		}
	}
	
	@RequestMapping("/startVote")
	public String startVote(String name,String[] keys,String[] notes){
		Integer groupId = voteService.startVote(name,keys, notes);
		if (groupId == -1){
			return "redirect:/admin/vote/index";
		} else {
			return "redirect:/admin/vote/getVoteDetail?groupId="+groupId;
		}
	}
	
	@RequestMapping("/stopVote")
	public String stopVote(){
		Integer groupId = voteService.stopVote();
		if (groupId == -1){
			return "redirect:/admin/vote/index";
		} else {
			return "redirect:/admin/vote/getVoteDetail?groupId="+groupId;
		}
	}
	
	@RequestMapping("/getVoteDetail")
	public String getVoteDetail(Model model,Integer groupId){
		VoteGroup group = voteService.getVoteGroup(groupId);
		model.addAttribute("group", group);
		return "vote/detail_group";
	}
	
	@RequestMapping("/getVoteItemDetail")
	public String getVoteItemDetail(Model model,Integer itemId){
		VoteItem voteItem = voteService.getVoteItem(itemId);
		model.addAttribute("item", voteItem);
		return "vote/detail_item";
	}
	
	
}
